package com.dingding.transfer.stamper.demo;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.dingding.transfer.stamper.constant.DingDingUrl;
import com.dingding.transfer.stamper.constant.StamperUrl;
import com.dingding.transfer.stamper.dto.EmployeeOnJob;
import com.dingding.transfer.stamper.util.DingCallbackCrypto;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiReportCreateRequest;
import com.dingtalk.api.request.OapiReportCreateRequest.OapiReportContentVo;
import com.dingtalk.api.response.OapiReportCreateResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangxiyuan
 * @description
 * @createDate 2021/3/30 9:58
 **/
@Slf4j
public class HttpRequestDemo {

    public static String getEmployeeINfo(String accessToken){
        EmployeeOnJob employeeOnJob = new EmployeeOnJob();
        List<String> statusList = new ArrayList<>();
        statusList.add("2");
        statusList.add("3");
        employeeOnJob.setOffset(0);
        employeeOnJob.setSize(20);
        employeeOnJob.setStatusList(statusList.stream().collect(Collectors.joining(",")));

        String json = JSONUtil.toJsonStr(employeeOnJob);
        System.out.println(json);

        String dingUrl = DingDingUrl.QUERY_ON_JOB + "?access_token=" + accessToken;
        String result = HttpUtil.post(dingUrl, json);
        System.out.println(result);
        return result;
    }

    public static String getAccessToken(String appkey,String appsecret){
        StringBuffer buffer = new StringBuffer(DingDingUrl.GET_ACCESS_TOKEN);
        buffer.append("?appkey=").append(appkey);
        buffer.append("&appsecret=").append(appsecret);
        String result = HttpUtil.get(buffer.toString());
        log.info(result);
        return result;
    }

    public static String getUserInfo(String accessToken){
        String dingUrl = DingDingUrl.GET_USER_INFO + "?access_token=" + accessToken;
        Map<String,Object> para = new HashMap<>();
        para.put("userid","manager9942");
        String result = HttpUtil.get(dingUrl,para);
        System.out.println(result);
        return result;
    }

    public static String getDeptList(String accessToken){
        String dingUrl = DingDingUrl.GET_DEPT_LIST + "?access_token=" + accessToken;
        Map<String,Object> para = new HashMap<>();
        para.put("dept_id","1");
        para.put("cursor",0);
        para.put("size",100);
        String result = HttpUtil.post(dingUrl,para);
        System.out.println(result);
        return result;
    }

    public static String getUserList(String accessToken){
        String dingUrl = DingDingUrl.GET_USER_LIST + "?access_token=" + accessToken;
        Map<String,Object> para = new HashMap<>();
        para.put("dept_id","478092051");
        para.put("cursor",0);
        para.put("size",100);
        String result = HttpUtil.post(dingUrl,para);
        System.out.println(result);
        return result;
    }

    public static String getApplyInstanceInfo(String accessToken){
        String dingUrl = "https://oapi.dingtalk.com/topapi/processinstance/get" + "?access_token=" + accessToken;
        Map<String,Object> para = new HashMap<>();
        para.put("process_instance_id","65654b25-1366-4169-a82a-6ed5e813c55b");
 /*       para.put("offset",0);
        para.put("size",100);*/
        String result = HttpUtil.post(dingUrl,para);
        System.out.println(result);
        return result;
    }

    public static void fingerDel(String accessToken){
        String dingUrl = StamperUrl.PATH_FINGER_DEL;
        Map<String,Object> para = new HashMap<>();
        para.put("userId","1");
        para.put("uuid","0X2900403339470B33303437");
        HttpResponse httpResponse = HttpRequest.post(dingUrl)
                .header("tToken", accessToken)
                .form(para)
                .execute();
        System.out.println(httpResponse.body());
    }

    public static void  recordList(String accessToken){
        String dingUrl = StamperUrl.PATH_RECORD_LIST;
        Map<String,Object> para = new HashMap<>();
        para.put("uuid",new String[]{"0X2900403339470B33303437"});
        para.put("applicationId",new String[]{"1"});
        para.put("page",true);
        para.put("pageNum",0);
        para.put("pageSize",100);
        HttpResponse httpResponse = HttpRequest.post(dingUrl)
                .header("tToken", accessToken)
                .form(para)
                .execute();
        System.out.println(httpResponse.body());
    }

    public static String getTemplete(String accessToken){
        String dingUrl = DingDingUrl.GET_INFO_TEMPLETE + "?access_token=" + accessToken;
        Map<String,Object> para = new HashMap<>();
        para.put("userid","01066836264");
        para.put("template_name","用印记录");
        String result = HttpUtil.post(dingUrl,para);
        System.out.println(result);
        return result;
    }

    /**
     * 创建日志
     * @return
     */
    public static void createInfo(String accessToken) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(DingDingUrl.CREATE_INFO);
        OapiReportCreateRequest req = new OapiReportCreateRequest();
        OapiReportCreateRequest.OapiCreateReportParam createReportParam = new OapiReportCreateRequest.OapiCreateReportParam();
        List<OapiReportCreateRequest.OapiReportContentVo> content = new ArrayList<>();
        OapiReportContentVo obj1 = new OapiReportContentVo();

        obj1.setSort(0L);
        obj1.setType(1L);
        obj1.setContentType("markdown");
        obj1.setContent("2021-04-17");
        obj1.setKey("使用日期");

        OapiReportContentVo obj2 = new OapiReportContentVo();

        obj2.setSort(1L);
        obj2.setType(1L);
        obj2.setContentType("markdown");
        obj2.setContent("王溪源");
        obj2.setKey("用章人");

        OapiReportContentVo obj3 = new OapiReportContentVo();

        obj3.setSort(2L);
        obj3.setType(1L);
        obj3.setContentType("markdown");
        obj3.setContent("印章(新17)");
        obj3.setKey("加盖印章");

        OapiReportContentVo obj4 = new OapiReportContentVo();

        obj4.setSort(3L);
        obj4.setType(1L);
        obj4.setContentType("markdown");
        obj4.setContent("255");
        obj4.setKey("当前次数");

        OapiReportContentVo obj5 = new OapiReportContentVo();

        obj5.setSort(4L);
        obj5.setType(1L);
        obj5.setContentType("markdown");
        obj5.setContent("杭州");
        obj5.setKey("地址");

        OapiReportContentVo obj6 = new OapiReportContentVo();

        obj6.setSort(6L);
        obj6.setType(1L);
        obj6.setContentType("markdown");
        obj6.setContent("[使用记录1](https://yunxi.qstamper.com/group1/default/20210407/14/48/0/2-224-0-0-0-1617778122802.jpg)");
        obj6.setKey("盖章图片");

        content.add(obj1);
        content.add(obj2);
        content.add(obj3);
        content.add(obj4);
        content.add(obj5);
        content.add(obj6);
        ArrayList<String> toUserids = new ArrayList<>();
        toUserids.add("user123");
        createReportParam.setToUserids(toUserids);
        createReportParam.setContents(content);
        createReportParam.setTemplateId("178abe9f81f344dfcf592194b4c97924");
        createReportParam.setToChat(true);
        createReportParam.setDdFrom("report");
        createReportParam.setUserid("manager9942");
        req.setCreateReportParam(createReportParam);
        OapiReportCreateResponse rsp = client.execute(req, accessToken);
        System.out.println(rsp.getBody());
    }

    public static void getDecryptMsg() throws DingCallbackCrypto.DingTalkEncryptException {
        DingCallbackCrypto callbackCrypto = new DingCallbackCrypto("123456",
                "Yo3GflXS5sN8oljf3f2MebvxDAiOKa97yOt81DNuwGS", "dingkxo470xlq4jtzeoq");
        String encryptMsg = "pomX/iRMXetVhFnA81byXsVFmStiegJPO6ZqOQOn+UZ7O6YEITI0SMjPtSo95HJzsoZ5ocRcn5LpliKbq5Zh7Qj0kDHoHK3qkIwZP7zL3FuN47Mu62HSlj6nJzJG68hw";
        final String decryptMsg = callbackCrypto.getDecryptMsg("1511e8921c4bcbb83dcb3b0666b2f0c577827c2d", "1617872542499", "OZoGEGEj", encryptMsg);
        System.out.println(decryptMsg);
    }

    public static void main(String[] args) throws Exception {
        //getAccessToken("dingipzstjaia6lpn8hy","ll37kQ6moB4uUJre9O_EYWBvM0WpQ9d3ZT2qdgNNiO9XecyE7QCXKYCbE2VGyYJ1");
        //getEmployeeINfo("497ee8eec2c43c00b198cd179a5da792");
        //getUserInfo("6d0bafa68e3f3a5e8ddb3d373a689e01");
        /*String deptList = getDeptList("7b6e1829a7fc398e8dd7d7b6aea5e144");
        List<DeptInfo> list = new ArrayList<>();
        String[] deptInfos = deptList.split(",");
        for (String deptInfo :
                deptInfos) {
            list.add(GsonUtil.fromJson(deptList, DeptInfo.class));
        }*/

        //getUserList("7b6e1829a7fc398e8dd7d7b6aea5e144");
        //getApplyInstanceInfo("55905bace9c535269098c5a3405ee93b");
        //fingerDel("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTg0NzUxNTEsInVzZXJuYW1lIjoiMjcifQ.VkQPfwfDrtvBw6jya5EJ1VTTm75gBCQaNllWkis9tfM");
        // 178a6bf2b988a8f0750e3b8446592d5e
        getTemplete("f04a1e3d381b3522843e12a9305dd4b5");
        //recordList("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTgwMTc0NDYsInVzZXJuYW1lIjoiMjcifQ._HNF_z656Ltu4KEjja_b8fN5bgnLhHEyuSxbBD0R6Xc");
        //createInfo("65abe9e4b54d392db8eaa3dabdabe006");
        /*boolean after = LocalDateTime.now().minusSeconds(10).isAfter(LocalDateTime.parse("2021-04-08 16:35:50", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(after);*/
        //getDecryptMsg();
    }
}
